package com.js.lottery.domin.strategy.service.algorithm.impl;

import com.js.lottery.domin.strategy.service.algorithm.BaseAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author:JiangSong
 * @Date:2023/11/10
 **/

@Component("singleRateRandomDrawAlgorithm")
public class SingleRateRandomDrawAlgorithm extends BaseAlgorithm {

    Logger log = LoggerFactory.getLogger(SingleRateRandomDrawAlgorithm.class);

    @Override
    public String randomDraw(Long strategyId, List<String> excludeAwardIds) {

        // 获取策略对应的元祖
        String[] rateTuple = super.rateTupleMap.get(strategyId);
        // assert rateTuple != null;
        if (rateTuple == null) {
            log.warn("rateTuple is null, strategyId = {}", strategyId);
            return null;
        }

        // 随机索引
        int randomVal = this.generateSecureRandomIntCode(100);
        int idx = super.hashIdx(randomVal);

        // 返回结果
        String awardId = rateTuple[idx];

        // 如果中奖ID命中排除奖品列表，则返回NULL
        if (excludeAwardIds.contains(awardId)) {
            return null;
        }

        return awardId;
    }
/*
    @Override
    public String randomDraw(Long strategyId, List<String> excludeAwardIds) {

        // 获取策略对应的元祖
        String[] rateTuple = super.rateTupleMap.get(strategyId);

        // assert rateTuple != null;
        if (rateTuple == null) {
            log.warn("rateTuple is null, strategyId = {}", strategyId);
            return null;
        }

        // 随机索引
        int randomVal = new SecureRandom().nextInt(100) + 1;
        int idx = super.hashIdx(randomVal);

        // 返回结果
        String awardId = rateTuple[idx];
        if (excludeAwardIds.contains(awardId)) {
            return "未中奖";
        }

        return awardId;
    }
    */

}
